import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:love_app/common/su.dart';
import 'package:love_app/config/prefs.dart';
import 'package:love_app/providers/share_provider.dart';
import 'package:love_app/routes/application.dart';
import 'package:love_app/routes/routes.dart';
import 'package:provider/provider.dart';

class TestCenter extends StatefulWidget {
  @override
  _TestCenterState createState() => _TestCenterState();
}

class _TestCenterState extends State<TestCenter> {
  ShareProvider _shareProvider;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    _shareProvider = Provider.of<ShareProvider>(context, listen: false);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: Su().appBar(
        "测试中心",
        colors: _shareProvider.themeColor,
      ),
      body: _body(),
    );
  }

  Widget _body() {
    return Scrollbar(
      // 显示进度条
      child: SingleChildScrollView(
        padding: EdgeInsets.all(16.0),
        child: Center(
          child: _column(),
        ),
      ),
    );
  }

  Widget _column(){
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        _copyToken(),
        _loginQuit(),
        _appTest(),
      ],
    );
  }

  // 复制token
  Widget _copyToken(){
    return RaisedButton(
      child: Text("复制token"),
      onPressed: (){
        ClipboardData data = ClipboardData(text: _shareProvider.token);
        Clipboard.setData(data);
      },
    );
  }

  // 退出登录
  Widget _loginQuit(){
    return RaisedButton(
      child: Text("退出登录"),
      onPressed: ()async{
        _shareProvider.cleanModel(); // 清除共享模型
        var pre = await Su().presObject(); // 获取持久化token
        pre.remove(Prefs.token); // 清除token
        Application.router.navigateTo(context, Routes.loginPage, clearStack: true);
      },
    );
  }

    // 退出登录
  Widget _appTest(){
    return RaisedButton(
      child: Text("测试Page"),
      onPressed: ()async{
        Application.router.navigateTo(context, Routes.appTest);
      },
    );
  }
}
